package com.example.spring5demo;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 结合 java8 线程安全
 */
public class NewDateUtil {


    private static  DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /**
     * 毫秒 赚
     * @param time
     * @return
     */
    public  static  String formatTimestamp(Long time){
        return sdf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
    }

    public  static  String formatDate(Date date){
        return  sdf.format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
    }

    public  static  Date parseString(String date){
        return Date.from(LocalDateTime.parse(date,sdf).atZone(ZoneId.systemDefault()).toInstant());
    }


    public static void main(String[] args) {
        System.out.println("----------------------");
       long time = 1532353895000L;
       System.out.println(formatTimestamp(time));

        Date date = new Date();
        System.out.println(formatDate(date));

        String str = "2018-07-23 21:51:35";
        System.out.println(parseString(str));
        System.out.println("----------------------");
    }
}
